package com.hook.analyse;
import android.app.*;
import android.os.*;
import android.widget.*;
import com.hook.analyse.model.*;
import java.util.*;
import android.view.*;
import com.hook.analyse.util.*;
import android.content.*;
import android.widget.AdapterView.*;

public class HookActivity extends Activity
{

	private ListView lv_app_list;
    private AppAdapter mAppAdapter;
    public Handler mHandler = new Handler();
	private ProgressDialog waitingDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hook_main);
		lv_app_list =  findViewById(R.id.app_list);
        mAppAdapter = new AppAdapter();
        lv_app_list.setAdapter(mAppAdapter);
		initAppList();

		lv_app_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					// TODO: Implement this method
					AppInfo app=(AppInfo) mAppAdapter.getItem(p3);

					HDb hdb=HDb.getInstance(Config.hookEventPath);
					List<HookEventBean> dataBean=hdb.read(HookEventBean.class);
					List<String> d=new ArrayList<>();

					for (HookEventBean hook:dataBean)
					{
						if (hook.getPackageName().equals(app.packageName))
						{
							d.add(hook.getMethod());

						}
					}
					String[] strings = new String[d.size()];
					boolean[] bbb = new boolean[d.size()];



					d.toArray(strings);
					showMultiChoiceDialog(app.packageName, strings, bbb);

				}


			});


	}

	ArrayList<Integer> yourChoices = new ArrayList<>();
	private void showMultiChoiceDialog(final String packageName, final String[] items, final boolean[] status)
	{

		yourChoices.clear();
		AlertDialog.Builder multiChoiceDialog = 
			new AlertDialog.Builder(HookActivity.this);
		multiChoiceDialog.setTitle("Hook事件");
		multiChoiceDialog.setMultiChoiceItems(items, status,
			new DialogInterface.OnMultiChoiceClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which,
									boolean isChecked)
				{
					if (isChecked)
					{
						yourChoices.add(which);
					}
					else
					{
						yourChoices.remove(which);
					}
				}
			});
		multiChoiceDialog.setPositiveButton("删除", 
			new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which)
				{

					HDb hdb=HDb.getInstance(Config.hookEventPath);
					List<HookEventBean> dataBean=hdb.read(HookEventBean.class);

					List<HookEventBean> data=new ArrayList<>();

					for (HookEventBean hook:dataBean)
					{

						for (int m:yourChoices)
						{

							if (hook.getPackageName().equals(packageName) && items[m].equals(hook.getMethod()))
							{
								continue;
							}
							data.add(hook);

						}
					}
					hdb.write(data);
					initAppList();
				}
			});
		multiChoiceDialog.show();
	}



	private void showWaitingDialog()
	{
		waitingDialog = 
			new ProgressDialog(HookActivity.this);
		waitingDialog.setTitle("提示");
		waitingDialog.setMessage("加载App列表中...");
		waitingDialog.setIndeterminate(true);
		waitingDialog.setCancelable(false);
		waitingDialog.show();
	}

	private void initAppList()
	{
		showWaitingDialog();
        new Thread(){
            @Override
            public void run()
			{
                super.run();
                //扫描得到APP列表
                List<AppInfo> appInfos = Apptool.scanLocalInstallAppList(HookActivity.this.getPackageManager());
				HDb hdb=HDb.getInstance(Config.hookEventPath);
				List<HookEventBean> dataBean=hdb.read(HookEventBean.class);
				final List<AppInfo> data = new ArrayList<>();
				for (HookEventBean h:dataBean)
				{
					for (AppInfo A : appInfos)
					{
						if (h.getPackageName().equals(A.packageName) && !data.contains(A))
						{
							data.add(A);
						}
					}
				}


                mHandler.post(new Runnable() {
						@Override
						public void run()
						{
							mAppAdapter.setData(data);
							mAppAdapter.notifyDataSetChanged();
							waitingDialog.dismiss();
						}
					});
            }
        }.start();
    }


    class AppAdapter extends BaseAdapter
	{

        List<AppInfo> myAppInfos = new ArrayList<AppInfo>();

        public void setData(List<AppInfo> myAppInfos)
		{
            this.myAppInfos = myAppInfos;
            notifyDataSetChanged();
        }

        public List<AppInfo> getData()
		{
            return myAppInfos;
        }

        @Override
        public int getCount()
		{
            if (myAppInfos != null && myAppInfos.size() > 0)
			{
                return myAppInfos.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position)
		{
            if (myAppInfos != null && myAppInfos.size() > 0)
			{
                return myAppInfos.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position)
		{
            return 0;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent)
		{
            ViewHolder mViewHolder;
            AppInfo myAppInfo = myAppInfos.get(position);
            if (convertView == null)
			{
                mViewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.appinfo, null);
                mViewHolder.iv_app_icon = (ImageView) convertView.findViewById(R.id.iv_app_icon);
                mViewHolder.tx_app_name = (TextView) convertView.findViewById(R.id.tv_app_name);
				mViewHolder.tx_package_name = (TextView) convertView.findViewById(R.id.tv_package_name);
				convertView.setTag(mViewHolder);
            }
			else
			{
                mViewHolder = (ViewHolder) convertView.getTag();
            }
            mViewHolder.iv_app_icon.setImageDrawable(myAppInfo.img);
            mViewHolder.tx_app_name.setText(myAppInfo.appName);
			mViewHolder.tx_package_name.setText(myAppInfo.packageName);

            return convertView;
        }

        class ViewHolder
		{

            ImageView iv_app_icon;
            TextView tx_app_name;
			TextView tx_package_name;

        }
    }
}
